﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace Housing
{
    public class GovernmentCheck
    {
        private int _gov_id;
        private int _gov_noid;
        private string _gov_checktime;
        private string _gov_signature;
        private string _gov_responame;
        private string _gov_applyid;

        public string Gov_applyid
        {
            get { return _gov_applyid; }
            set { _gov_applyid = value; }
        }

        public int gov_id
        {
            get { return _gov_id; }
            set { _gov_id = value; }
        }
        public int gov_noid
        {
            get { return _gov_noid; }
            set { _gov_noid = value; }
        }
        public string gov_checktime
        {
            get { return _gov_checktime; }
            set { _gov_checktime = value; }
        }
        public string gov_signature
        {
            get { return _gov_signature; }
            set { _gov_signature = value; }
        }
        public string gov_responame
        {
            get { return _gov_responame; }
            set { _gov_responame = value; }
        }

        public GovernmentCheck()
        {
        }

        public GovernmentCheck(int govId)
        {
            GetGovernmentCheckBygovId(govId);
        }

        public void GetGovernmentCheckBygovId(int govId)
        {
            string query = "SELECT * FROM GovernmentCheck WHERE gov_id=" + govId;

            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.Text, query);

            if (dr.Read())
            {
                _gov_id = (int)dr[0];
                _gov_noid = (int)dr[1];
                _gov_checktime = Utils.GetString(dr[2]);
                _gov_signature = Utils.GetString(dr[3]);
                _gov_responame = Utils.GetString(dr[4]);
            }

            dr.Close();
        }

        public int AddNewGovernmentCheck(GovernmentCheck gov)
        {
            string query = "INSERT INTO GovernmentCheck(gov_noid, gov_checktime, gov_signature, gov_responsname,applyid)"
                + "VALUES(" + gov.gov_noid + ",'" + gov.gov_checktime + "','" + gov.gov_signature + "','" + gov.gov_responame + "',"+gov.Gov_applyid+")";

            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, query);
        }

        public int UpdateGovernmentCheckBygovId(GovernmentCheck gov)
        {
            string query = "UPDATE GovernmentCheck SET gov_noid=" + gov.gov_noid + ",gov_checktimg='" + gov.gov_checktime + "',"
                + "gov_signature='" + gov.gov_signature + "', gov_responsname='" + gov.gov_responame + "'"
                + "WHERE gov_id=" + gov.gov_id;

            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, query);
        }

        public int DeleteGovernmentCheckBygovId(int govId)
        {
            string query = "DELETE FROM GovernmentCheck WHERE gov_id=" + govId;

            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, query);
        }

        public bool AddGovCheck(string applyid, string name, string responsename, string date)
        {
            string cmd = "SELECT COUNT(*) FROM GovernmentCheck WHERE applyid=" + applyid;
            int succornot = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, CommandType.Text, cmd));
            if (succornot == 0)
            {
                GovernmentCheck governmentcheck = new GovernmentCheck();
                governmentcheck.gov_noid = 0;
                governmentcheck.gov_checktime = date;
                governmentcheck.gov_responame = responsename;
                governmentcheck.gov_signature = name;
                governmentcheck.Gov_applyid = applyid;
                bool succ=Convert.ToBoolean(governmentcheck.AddNewGovernmentCheck(governmentcheck));
                return succ;
            }
            else
                return false;
        }
    }
}